package com.woniuxy.factory.controller;

import com.woniuxy.factory.entity.Process;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/process")
public class ProcessController {
    
    @Autowired
    ProcessService ps;

    //查询所有的工序信息
    @ResponseBody
    @RequestMapping("/selectprocess")
    public ResponseResult selectReworks(Process process, int page, int limit){
        System.out.println(process);
        //查询所有工序信息
        List<Process> lr = ps.selectProcessAll(process,page,limit);
        System.out.println(lr);
        //查询所有工序的总条数
        int count = ps.selectProcessCounts(process);
        ResponseResult rr = new ResponseResult(0,"",count,lr);
        return rr;
    }

    //添加工序
    @RequestMapping("/addprocess")
    @ResponseBody
    public ResponseResult addProcess(Process p){
        ps.insertProcess(p);
        ResponseResult rr = new ResponseResult(0,"添加成功",0,null);
        return rr;
    }

    //编辑当前id工序数据回显
    @ResponseBody
    @RequestMapping("/processinfoecho")
    public ResponseResult processEcho(String pssId){
        Process process = ps.selectProcessById(pssId);
        ResponseResult rr = new ResponseResult(0,"",0,process);
        return rr;
    }

    //修改工序信息
    @RequestMapping("/updateprocess")
    @ResponseBody
    public ResponseResult updateProcess(Process process){
        ResponseResult rr;
        try{
            ps.updateProcess(process);
            rr = new ResponseResult(0,"修改成功",0,null);
        }catch (Exception e){
            rr = new ResponseResult(1,"修改失败",0,null);
        }
        return rr;
    }

    //删除工序信息
    @ResponseBody
    @RequestMapping("deleteprocess")
    public ResponseResult deleteProcessById(int id){
        ResponseResult rr = null;
        try {
            ps.deleteProcessById(id);
            rr = new ResponseResult(0,"删除成功",0,null);
        }catch (Exception e){
            rr = new ResponseResult(1,"删除失败，请联系管理员！",0,null);
        }
        return rr;
    }

    //删除选中工序
    @RequestMapping(value="/deletepitchon")
    @ResponseBody
    public ResponseResult deletePitchOnById(@RequestParam("isStr[]") int[] pid){
        ResponseResult rr;
        try{
            ps.deleteProcessByIdsService(pid);
            rr= new ResponseResult(0,"删除成功",0,null);
        }catch(Exception e){
            rr= new ResponseResult(1,"删除失败,请联系管理员",0,null);
        }
        return rr;
    }
}
